package com.YOUtils.Base.Characher;

import com.YOUtils.YException.YNullPointerException;

/**
 * Created on 2023/6/14
 *
 * @Time 18:39
 * @Program YOUilt
 * @Author YoHaNe/XiaoShuai
 */
public class YoCharUtils {

    private final static int UP = 223;
    private final static int LOW = 32;

    public static boolean isEmpty(char c){
        return (c == 32 || c == 0);
    }

    public static int count(char[] chars,char mate){
        if(chars == null || chars.length <= 0 || mate == '\0')
            throw new YNullPointerException();
        int r = 0;
        for(char c : chars){
            if(c == mate)
                r++;
        }
        return r;
    }

    public static char toUp(char c){
        if(c < 97 || c > 122)
            return c;
        return (char) (c&UP);
    }

    public static char toLow(char c){
        if(c < 65 || c > 90)
            return c;
        return (char) (c|LOW);
    }

    public static boolean isChinese(char c){
        if(isEmpty(c))
            return false;
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS);
    }
}
